mod utils;
use core::str;
use wasm_bindgen::prelude::*;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

/// # 映射js中的alert方法
#[wasm_bindgen]
extern {
  fn alert(s: &str);
}

// #[wasm_bindgen]
// pub fn fetch(url: &str) -> String {
//   utils::set_panic_hook();
//   let ret = utils::request_url(url);
//   match ret {
//     Ok(data) => {
//       return String::from(data.get("body").unwrap());
//     },
//     Err(_) => {
//       return String::new();
//     }
//   }
//   String::new()
// }

/// 设置值，只接受字符串类型
#[wasm_bindgen]
pub fn set_item(key: &str, value: &str) {
  utils::set_panic_hook();
  utils::write_to_file(key, value);
}

/// 获取值，返回字符串类型
#[wasm_bindgen]
pub fn get_item(key: &str) -> String {
  utils::set_panic_hook();
  return utils::read_from_file(key);
}

/// aes128+base64加密，预置了key和iv
#[wasm_bindgen]
pub fn decode_aes(key: &str) -> String {
  utils::set_panic_hook();
  utils::aes_decode(key)
}

/// aes128+base64解密，预置了key和iv
#[wasm_bindgen]
pub fn encode_aes(key: &str) -> String {
  utils::set_panic_hook();
  utils::aes_encode(key)
}

/// heic图片转换为base64
#[wasm_bindgen]
pub fn heic_to_jpg(buf: Vec<u8>) -> Vec<u8> {
  utils::set_panic_hook();
  utils::heic2jpg(buf)
}
